#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/2/24
# @Author :cjx
# @File   :io_status.py

import can
import time
import numpy as np
import pymysql

con = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    password='!qAz2WsX3edc',
    db='ph',
    charset='utf8'
)
cur = con.cursor()

address = "HCJM001"



def io_status():
    while 1:
        global var1, var2, data_do, data_di  # 类型为list
        global falling_edge, rising_edge
        global di_location, do_location

        edge_reset = cur.execute("SELECT edge_reset FROM edge_reset order by id desc limit 1")
        edge_reset = int(cur.fetchone()[0])
        if edge_reset == 1:
            edge = np.zeros(64, np.int8).reshape(2, 32)
            falling_edge = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                            0]
            time3 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
            cur.executemany(
                "INSERT edge_reset(time1,edge_reset) VALUE(%s,%s)",
                [(time3, 0)])

        inquiry_do = can.Message(arbitration_id=0x0201, data=[0x01], is_extended_id=False)  # 问询
        bus.send(inquiry_do)  # 0x0201 DO状态 返回513
        message_do = bus.recv(timeout=1)  # 接收数据
        type_id_do = message_do.arbitration_id  # 返回can报文类型
        if type_id_do == 513:
            data_do = '{:0>32b}'.format(int(bin(int.from_bytes(message_do.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            if var1 != data_do:
                var1 = data_do
                do_location = list(data_do)
                time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                cur.executemany(
                    "INSERT do_s(address,time,do_01,do_02,do_03,do_04,do_05,do_06,do_07,do_08,do_09,do_10,do_11"
                    ",do_12,do_13,do_14,do_15,do_16,do_17,do_18,do_19,do_20,do_21,do_22,do_23,do_24,do_25,do_26,do_27"
                    ",do_28,do_29,do_30,do_31,do_32) VALUE(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"
                    "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                    [(address, time1, do_location[0:1], do_location[1:2], do_location[2:3], do_location[3:4]
                      , do_location[4:5], do_location[5:6], do_location[6:7], do_location[7:8], do_location[8:9]
                      , do_location[9:10], do_location[10:11], do_location[11:12], do_location[12:13],
                      do_location[13:14]
                      , do_location[14:15], do_location[15:16], do_location[16:17], do_location[17:18],
                      do_location[18:19]
                      , do_location[19:20], do_location[20:21], do_location[21:22], do_location[22:23],
                      do_location[23:24]
                      , do_location[24:25], do_location[25:26], do_location[26:27], do_location[27:28],
                      do_location[28:29]
                      , do_location[29:30], do_location[30:31], do_location[31:32])])
                con.commit()
        time.sleep(0.1)

        inquiry_di = can.Message(arbitration_id=0x0301, data=[0x01], is_extended_id=False)
        bus.send(inquiry_di)  # 0x0301  返回769/0x0301 di状态
        message_di = bus.recv(timeout=1)  # 接收数据
        type_id_di = message_di.arbitration_id  # 返回can报文类型
        if type_id_di == 769:
            data_di = '{:0>32b}'.format(int(bin(int.from_bytes(message_di.data[0:4], "little")).replace('0b', ''), 2))[
                      ::-1]
            if data_di != var2:  # 差异检测，差异写入数据库
                for k in range(32):
                    if var2[k] == '0' and data_di[k] == '1':  # 32点上升沿检测,没有检测到不复位
                        edge[0, k] = 1  # 写入边缘检测edge[0]
                        edge[1, k] = 0  # 复位下降沿
                    if var2[k] == '1' and data_di[k] == '0':  # 32点下降沿检测,没有检测到不复位
                        edge[1, k] = 1  # 写入边缘检测edge[1]
                        edge[0, k] = 0  # 复位上升沿
                var2 = data_di  # 给中间值赋新值
                di_location = list(data_di)  # 字符串转列表输出
                time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                cur.executemany(
                    "INSERT di_s(address,time,di_01,di_02,di_03,di_04,di_05,di_06,di_07,di_08,di_09,di_10,di_11"
                    ",di_12,di_13,di_14,di_15,di_16,di_17,di_18,di_19,di_20,di_21,di_22,di_23,di_24,di_25,di_26,di_27"
                    ",di_28,di_29,di_30,di_31,di_32) VALUE(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"
                    "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                    [(address, time1, di_location[0:1], di_location[1:2], di_location[2:3], di_location[3:4]
                      , di_location[4:5], di_location[5:6], di_location[6:7], di_location[7:8], di_location[8:9]
                      , di_location[9:10], di_location[10:11], di_location[11:12], di_location[12:13],
                      di_location[13:14]
                      , di_location[14:15], di_location[15:16], di_location[16:17], di_location[17:18],
                      di_location[18:19]
                      , di_location[19:20], di_location[20:21], di_location[21:22], di_location[22:23],
                      di_location[23:24]
                      , di_location[24:25], di_location[25:26], di_location[26:27], di_location[27:28],
                      di_location[28:29]
                      , di_location[29:30], di_location[30:31], di_location[31:32])])
                con.commit()
                # print('下降沿', edge[1])
                # print('上升沿', edge[0])
                rising_edge = edge[0]
                falling_edge = edge[1]
                print(falling_edge)
                time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                cur.executemany(
                    "INSERT falling_edge_di(address,time,falling_edge_di01,falling_edge_di02,falling_edge_di03,"
                    "falling_edge_di04,falling_edge_di05,falling_edge_di06,falling_edge_di07,falling_edge_di08,"
                    "falling_edge_di09,falling_edge_di10,falling_edge_di11 "
                    ",falling_edge_di12,falling_edge_di13,falling_edge_di14,falling_edge_di15,falling_edge_di16,"
                    "falling_edge_di17,falling_edge_di18,falling_edge_di19,falling_edge_di20,falling_edge_di21,"
                    "falling_edge_di22,falling_edge_di23,falling_edge_di24,falling_edge_di25,falling_edge_di26,"
                    "falling_edge_di27 "
                    ",falling_edge_di28,falling_edge_di29,falling_edge_di30,falling_edge_di31,falling_edge_di32) "
                    "VALUE(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, "
                    "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                    [(address, time1, falling_edge[0:1][0], falling_edge[1:2][0], falling_edge[2:3][0],
                      falling_edge[3:4][0]
                      , falling_edge[4:5][0], falling_edge[5:6][0], falling_edge[6:7][0], falling_edge[7:8][0],
                      falling_edge[8:9][0]
                      , falling_edge[9:10][0], falling_edge[10:11][0], falling_edge[11:12][0], falling_edge[12:13][0],
                      falling_edge[13:14][0]
                      , falling_edge[14:15][0], falling_edge[15:16][0], falling_edge[16:17][0], falling_edge[17:18][0],
                      falling_edge[18:19][0]
                      , falling_edge[19:20][0], falling_edge[20:21][0], falling_edge[21:22][0], falling_edge[22:23][0],
                      falling_edge[23:24][0]
                      , falling_edge[24:25][0], falling_edge[25:26][0], falling_edge[26:27][0], falling_edge[27:28][0],
                      falling_edge[28:29][0]
                      , falling_edge[29:30][0], falling_edge[30:31][0], falling_edge[31:32][0])])
                con.commit()
        time.sleep(0.1)


if __name__ == "__main__":
    # message_di = 0
    global manual_control_status, remote_maintenance_status, automatic_cleaning_status, do_location
    var1, var2 = '00000000000000000000000000000000', '00000000000000000000000000000000'
    data_do, data_di = 0, 0
    do_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    di_location = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    falling_edge = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                    '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    rising_edge = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
                   '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
    edge = np.zeros(64, np.int8).reshape(2, 32)  # DI上升/下降沿检测,edge[0,0]第1个up，edge[1,0]第1个down
    bus = can.Bus(interface='socketcan', channel='can0', receive_own_messages=eval('False'))
    io_status()
